package jwj8508.mvc.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jwj8508.mvc.dao.BoardDAO;
import jwj8508.mvc.model.BoardVO;

public class WriteBoardAction implements ActionHandler {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		String viewPage = "f|/WEB-INF/jsp/tiles/mvc.jsp";		
		
		String method = req.getMethod();
		if (method.equalsIgnoreCase("get")) {
			req.setAttribute("action", "../board/write.jsp");
		} else if (method.equalsIgnoreCase("post")) {
			
			req.setCharacterEncoding("UTF-8");
			BoardVO b = new BoardVO();
			b.setTitle(req.getParameter("title"));
			b.setUid(req.getParameter("uid"));
			b.setContents(req.getParameter("contents"));
			BoardDAO.writeBoard(b);
			viewPage = "r|listBoard.mvc";
			req.setAttribute("action", "../board/list.jsp");
		}
		return viewPage;
	}

}
